package com.alogic.validator.impl.pwd;

import com.alogic.validator.Validator;
import com.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * 验证是否包含某个动态值，比如说用户名
 *
 * @version 1.6.13.25 [20201110 duanyy] <br>
 * - 修复validator的问题
 */
public class Contain extends Validator.Abstract{
    protected String $key;
    @Override
    public void configure(Properties p) {
        super.configure(p);
        $key = PropertiesConstants.getRaw(p,"key",$key);
    }

    @Override
    public boolean check(String value, Properties ctx,boolean throwException) {
        boolean result = true;
        String key = PropertiesConstants.transform(ctx,$key,"");
        if (StringUtils.isNotEmpty(key)){
            result = !value.contains(key);
        }

        if (!result && throwException){
            throw new BaseException(getCode(ctx),getMessage(ctx));
        }
        return result;
    }

    @Override
    public boolean check(String value, Properties ctx, Listener listener) {
        boolean result = true;
        String key = PropertiesConstants.transform(ctx,$key,"");
        if (StringUtils.isNotEmpty(key)){
            result = !value.contains(key);
        }

        if (listener != null){
            listener.result(result,value,this,ctx);
        }

        return result;
    }
}